package main

import (
	"fmt"
	"strconv"
	"time"
)

func main() {
	//timeTimeType()
	//sleepTest()
	//timestampTest()
	test01()
}

func timeTimeType() {
	// 初识time
	var now = time.Now()
	fmt.Printf("now=%v, now type=%T", now, now)

	// 通过now可以获得当前的年月日
	fmt.Printf("年：%v\n", now.Year())
	fmt.Printf("月：%v\n", now.Month())
	fmt.Printf("日：%v\n", now.Day())
	fmt.Printf("时：%v\n", now.Hour())
	fmt.Printf("分：%v\n", now.Minute())
	fmt.Printf("秒：%v\n", now.Second())

	// 格式化输出
	fmt.Printf("当前时间：%d/%d/%d %d:%d:%d\n",
		now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
	// 格式化输出2
	fmt.Println(now.Format("2006/01/02 15:04:05"))
	fmt.Println(now.Format("2006/01/02"))
	fmt.Println(now.Format("15:04:05"))
}

func sleepTest() {
	for i := 0; i < 10; i++ {
		time.Sleep(time.Second / 20)
		time.Sleep(50 * time.Millisecond)
		fmt.Println(i)
	}
	fmt.Println(time.Second / 20)
}

func timestampTest() {
	now := time.Now()
	// 秒级时间戳
	fmt.Println("Unix timestamp: ", now.Unix())
	// 毫秒级时间戳
	fmt.Println("UnixNano timestamp: ", now.UnixNano())
}

func test01() {
	// 获取下列字符串拼接的时间
	start := time.Now().UnixNano()
	str := ""
	for i := 0; i < 10000; i++ {
		str += strconv.Itoa(i)
	}
	end := time.Now().UnixNano()
	fmt.Printf("执行字符串拼接花费了 %d 纳秒", end-start)
}
